#!/usr/bin/python3

import os
import logging
import logging.handlers
from pwdBase import getPwd
from baseSql import *
from dbPool import *

class UserInfo:
    def __init__(self, user, pwd="000000", write="N",
                 versionCreate="N", customerCreate="N", systemCreate ="N", productLine="N", exchange="N"):
        self.user=user
        self.pwd=getPwd(pwd)
        self.write=write
        self.versionCreate=versionCreate
        self.customerCreate=customerCreate
        self.systemCreate=systemCreate
        self.productLine=productLine
        self.exchange=exchange
        self.updateTime= ""



class CUserInfoMgr:
    tableTitle = ["用户名", "密码", "写权限", "版本管理权限", "客户管理权限",
                  "系统管理权限", "产品线管理权限", "交易所管理权限", "最近更新"
                  ]
    def __init__(self, log):
        self.log=log
        self.userInfo={"用户名" : UserInfo(user="")}


    # 添加用户，使用默密码 '000000'
    # 3251689763C90F53F93B839EAD35284E1DEE336B83424687B818BA79FAA0CA0F127C1A222CD0FB55694CF6238591DAC33749170D697E5BC58516B3756220C118
    def addUser(self, user, write, versionCreate, customerCreate, systemCreate, productLine, exchange):
        self.userInfo[user] = UserInfo(user=user, pwd=getPwd("000000"),write=write,
                                       versionCreate=versionCreate, customerCreate=customerCreate, systemCreate=systemCreate,
                                       productLine=productLine, exchange=exchange)
    # 暂时不用
    def updateOneUserInfo(self, user, pwd, write, versionCreate, customerCreate, systemCreate):
        if(pwd != ''):
            self.userInfo[user].pwd = getPwd(pwd)
        if (write != ''):
            self.userInfo[user].write = write
        if (versionCreate != ''):
            self.userInfo[user].versionCreate = versionCreate
        if (customerCreate != ''):
            self.userInfo[user].customerCreate = customerCreate
        if (systemCreate != ''):
            self.userInfo[user].systemCreate = systemCreate
        try:
            updateSql = "update " + t_user_info + " set pwd = " + self.userInfo[user].pwd + " where user_name = " + user
            execDBData(user, msg = "update user info", doSql=updateSql)
        except:
            self.log.log("update user info error:" + user, logging.ERROR)

    def reload(self):
        self.userInfo["admin"]=UserInfo(user="admin")
        # 5A38AFB1A18D408E6CD367F9DB91E2AB9BCE834CDAD3DA24183CC174956C20CE35DD39C2BD36AAE907111AE3D6ADA353F7697A5F1A8FC567AAE9E4CA41A9D19D
        self.userInfo["admin"].pwd = getPwd("admin")
        # 更新数据 密码是加密过的
        self.db.query(queryAllTableSql + t_user_info)
        userData = self.db.getData()
        for userParam in userData:
            user = UserInfo(user=userParam[0], pwd=userParam[1], write=userParam[2],
                            versionCreate=userParam[3], customerCreate=userParam[4], systemCreate=userParam[5],
                            productLine=userParam[6], exchange=userParam[7])

            self.log.log("load user " + str(user.user) + " info")
            if (user.user == "admin"):
                self.userInfo["admin"].pwd = user.pwd
                continue

            self.userInfo[user.user] = user

        # 加载用户完成
        self.log.log("load user info finish, user info count: " + str(len(self.userInfo)))

    def init(self,db):
        self.db = db
        self.reload()

    # 更新密码
    def updateUserPwd(self, user, newPwd):
        if (len(newPwd) < 6):
            return "密码太短"
        # 更新系统信息
        self.userInfo[user].pwd = getPwd(newPwd)
        # 更新数据库
        updateSql = "update " + t_user_info + " set pwd = " + self.userInfo[user].pwd + "where user_name = "  + user
        execDBData(self.db, user, msg = "update pwd", doSql = updateSql)
        return "update successful"





